//
//  HomeViewController.m
//  pz_live
//
//  Created by caiyc on 2018/9/17.
//  Copyright © 2018年 caiyc. All rights reserved.
//   caiyccaiyc

#import "HostHomeViewController.h"
#import "HostLive_Cell.h"
#import "HostLoginViewController.h"
#import "LiveViewController.h"
#import "LoginViewController.h"
#import "OYCountDownManager.h"
@interface HostHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *dataSouce;
//@property(nonatomic,strong)OYCountDownManager *kCountDownManagers;

@end

@implementation HostHomeViewController
//切换地区
- (IBAction)changeArea:(UIButton *)sender {
    LiveViewController *vc = [[LiveViewController alloc]init];
    [self pushSecondVC:vc];
//    AreaViewController *vc = [[AreaViewController alloc]init];
//    [self pushSecondVC:vc];
}
-(void)viewWillAppear:(BOOL)animated{
    if(![XTool GetDefaultInfo:USER_INFO]){
        [WToast showWithText:@"请先登录"];
        LoginViewController *vc = [[LoginViewController alloc]init];
        [self pushSecondVC:vc];
    }else{
        NSLog(@"用户信息===%@",[XTool GetDefaultInfo:USER_INFO]);
        NSDictionary *user_Dic = [XTool GetDefaultInfo:USER_INFO];
        NSURL *url = [NSURL URLWithString:user_Dic[@"logo"]];
        [self.logo sd_setImageWithURL:url];
        [self.titles setText:user_Dic[@"title"]];
        self.live_Id.text = user_Dic[@"id"];
        [self loadLiveLis];
        }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = viewcontrollerColor;
    self.tableView.backgroundColor = viewcontrollerColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.delegate   = self;
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadLiveLis)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.icon_Img.userInteractionEnabled = 1;
    [kCountDownManager start];
            self.dataSouce = [NSMutableArray array];
   // [self loadLiveLis];
    
    // Do any additional setup after loading the view from its nib.
}
//获取直播列表
-(void)loadLiveLis{
    [self.dataSouce removeAllObjects];
    NSDictionary *param = @{@"ac":@"store",@"op":@"activity",@"ctrl":@"live_merch"};
    [self weqPostHandle:param success:^(id response) {
        [self.tableView.mj_header endRefreshing];
        SLog(@"....res...%@",response);
        if(response){
            [kCountDownManager reload];
            [self.dataSouce addObjectsFromArray:response[@"data"]];
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
         [self.tableView.mj_header endRefreshing];
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"sells";
    HostLive_Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
        cell = [[[NSBundle mainBundle]loadNibNamed:@"HostLive_Cell" owner:self options:nil]lastObject];
          }
    cell.live=^(){
        LiveViewController *vc = [[LiveViewController alloc]init];
        vc.live_id = self.dataSouce[indexPath.row][@"id"];
       [self pushSecondVC:vc];
    };
    [cell bindData:self.dataSouce[indexPath.row]];
    return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSouce.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 320;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//    NSDictionary *dic = [self.dataSouce objectAtIndex:indexPath.row];
//    PlayViewController *vc = [[PlayViewController alloc]init];
//    vc.live_id = [NSString stringWithFormat:@"%@",dic[@"id"]];
//    [self pushSecondVC:vc];
    
    
//    LiveViewController *vc = [[LiveViewController alloc]init];
//    [self pushSecondVC:vc];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)exit:(id)sender {
    [XTool SaveDefaultInfo:nil Key:USER_INFO];
    [XTool SaveDefaultInfo:nil Key:Prole];
    LoginViewController *vc = [[LoginViewController alloc]init];
    [self pushSecondVC:vc];
}
@end
